trigger LeadsInput on Lead (after insert, after update) {
/*
    List<Lead> oldRecords = trigger.old;
    List<Lead> triggeredRecords = trigger.new;
    
    if (trigger.isBefore && !trigger.isUpdate) {
        Integer currentMinutes = Integer.valueOf(DateTime.now().format('m')) + 1;
        if (currentMinutes >= 60) {
            currentMinutes -= 60;
        }
        List<EZAssignJobIDs__c> jobRecords = [select JobID__c, Name from EZAssignJobIDs__c where Name = 'Update Leads - Before' limit 1];
        EZAssignJobIDs__c jobRecord;
        if (jobRecords.size() > 0) {
        	jobRecord = jobRecords[0];
        } else {
        	jobRecord = new EZAssignJobIDs__c();
            jobRecord.Name = 'Update Leads - Before';
            jobRecord.JobID__c = '000000000000000';
        }
        List<CronTrigger> scheduledJobs = [select Id from CronTrigger where Id = :jobRecord.JobID__c];
        
        if (scheduledJobs.size() == 0)
        {
        	String jobId = '';
        	
        	try
        	{
        		jobId = System.schedule('Update Leads - Before', '0 ' + currentMinutes + ' * * * ?', new SchedulerClass());
        	}
        	catch(Exception e)
        	{
        		System.debug('Error: Unable able to schedule job. ' + e);
        	}
            jobRecord.JobID__c = jobId;
            upsert jobRecord;
        }
    }
    
    if (trigger.isAfter || trigger.isUpdate) {
        Map<Id,Lead> oldRecordsMap = new Map<Id,Lead>();
        if (oldRecords != null) {
            for (Lead item: oldRecords) {
                oldRecordsMap.put(item.Id,item);
            }
        }
        
        List<QueueSetting__c> qsList = [select Id,Active__c,QueueId__c from QueueSetting__c where Active__c = true];
        Map<Id,QueueSetting__c> qsToQid = new Map<Id,QueueSetting__c>();
        for (QueueSetting__c item: qsList) {
            qsToQid.put(item.QueueId__c,item);
        }
        
        List<Lead> recordsToProcess = new List<Lead>();
        
        List<EZAssignSettings__c> afs = [select ObjectAPIName__c, ActiveFieldValues__c, ActiveFieldAPIName__c 
                                                from EZAssignSettings__c
                                                where ObjectAPIName__c = 'Lead' limit 1];
        String field;
        String value;
        if (afs.size() > 0) {
	        value = afs[0].ActiveFieldValues__c;
	        field = afs[0].ActiveFieldAPIName__c;
        }
        Boolean isActiveRecord;
        
        for (Lead item: triggeredRecords) {
            if (qsToQid.get(item.OwnerId) != null) {
                isActiveRecord = false;
                if (value != null) {
                    List<String> valueParts = ((String)value).split(',');
                    for (String val: valueParts) {
                        try {
	                        if (item.get(field) == val) {
	                            isActiveRecord = true;
	                        }
                        } catch (Exception ex) {}
                    }
                    if (isActiveRecord) {
                        recordsToProcess.add(item);
                    }
                }
            }
        }
*/        
        //ProcessRecords batchController = new ProcessRecords(recordsToProcess,false,true);
        Integer currentMinutes = Integer.valueOf(DateTime.now().format('m')) + 1;
        if (currentMinutes >= 60) {
            currentMinutes -= 60;
        }
        
        List<EZAssignJobIDs__c> jobRecords = [select JobID__c, Name from EZAssignJobIDs__c where Name = 'Update Leads - After' limit 1];
        EZAssignJobIDs__c jobRecord;
        if (jobRecords.size() > 0) {
        	jobRecord = jobRecords[0];
        } else {
        	jobRecord = new EZAssignJobIDs__c();
            jobRecord.Name = 'Update Leads - After';
            jobRecord.JobID__c = '000000000000000';
        }
        List<CronTrigger> scheduledJobs = [select Id from CronTrigger where Id = :jobRecord.JobID__c];
        
       if (scheduledJobs.size() == 0) 
        {
        	String jobId = '';
        	
        	try
        	{
        		jobId = System.schedule('Update Leads - After', '0 ' + currentMinutes + ' * * * ?', new SchedulerClass());
        	}
        	catch(Exception e)
        	{
        		System.debug('Error: Unable able to schedule job. ' + e);
        	}
        	
            jobRecord.JobID__c = jobId;
            upsert jobRecord;
        }
    //}
}